package com.viewpagerindicator.sample.fragents;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


import com.viewpagerindicator.sample.CutokApplication;
import com.viewpagerindicator.sample.R;
import com.viewpagerindicator.sample.SspDeviceActivity;
import com.viewpagerindicator.sample.adapters.DeviceListAdapter;
import com.viewpagerindicator.sample.view.CProgressDialog;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import dmax.dialog.SpotsDialog;


public final class FourthFragment extends Fragment  {
    private static final String KEY_CONTENT = "TestFragment:Content";

    public static FourthFragment newInstance(String content) {
        FourthFragment fragment = new FourthFragment();
        return fragment;
    }

    private ListView listView;
    private DeviceListAdapter adapter;
    private View listviewBg; //列表试图
    private View progressingBg;//等待试图;
    private View errormsgBg;//错误试图;
    TextView title ;
    TextView desc;
    TextView progressTitle;
    private List<BluetoothDevice>  bluetoothDeviceList=new ArrayList<>();

    private BluetoothAdapter mBluetoothAdapter;
    private boolean mScanning;

    private static final int REQUEST_ENABLE_BT = 1;
    // Stops scanning after 10 seconds.
    private static final long SCAN_PERIOD = 10000;
    Button buttonSearch;

    private View rootView=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fourth_layout,container,false);
        listView= (ListView) view.findViewById(R.id.first_layout_listview);
        listviewBg=  view.findViewById(R.id.list_bg);
        progressingBg=  view.findViewById(R.id.progress_bg);
        errormsgBg=  view.findViewById(R.id.errormsg_bg);
        buttonSearch= (Button) view.findViewById(R.id.btn_bluetooth_search);
        title= (TextView) view.findViewById(R.id.errormsg_title);
        desc= (TextView) view.findViewById(R.id.errormsg_desc);
        progressTitle= (TextView) view.findViewById(R.id.progress_bg_title);
        //获取bluetoothAdapter
        final BluetoothManager bluetoothManager =(BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
        //检查设备是否支持
        if (mBluetoothAdapter == null) {
            show(errormsgBg, getString(R.string.error_bluute_cantsupport), null);
            return view;
        }
        adapter=new DeviceListAdapter(getActivity(),R.layout.device_item,bluetoothDeviceList);
        listView.setAdapter(adapter);
        errormsgBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show(listviewBg);
                //开启蓝牙设备
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                // 设置蓝牙可见性，最多300秒
                intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
                getActivity().startActivity(intent);
            }
        });

        progressingBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bluetoothDeviceList.size() == 0) {
                    show(errormsgBg, getString(R.string.btn_bluetooth_search_no), null);
                } else {
                    show(listviewBg);
                }
            }
        });
        buttonSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), SspDeviceActivity.class);
                startActivity(intent);
            }
        });
        if(mBluetoothAdapter.isEnabled()){
            List<BluetoothDevice> list=getBonedList(mBluetoothAdapter);
            if(list!=null){
                bluetoothDeviceList.clear();
                bluetoothDeviceList.addAll(list);
                adapter.notifyDataSetChanged();
                show(listviewBg);
            }else{
                show(listviewBg);
            }
        }else{
            show(errormsgBg, getString(R.string.error_blute_close),getString(R.string.error_bluetooth_desc));
        }
        return view;
    }
    public List<BluetoothDevice> getBonedList(BluetoothAdapter bluetoothAdapter){
        Set<BluetoothDevice>  bluetoothDeviceSet=bluetoothAdapter.getBondedDevices();
        List<BluetoothDevice> list=new ArrayList<>();
        Iterator<BluetoothDevice> iterator=bluetoothDeviceSet.iterator();
        while(iterator.hasNext()){
            BluetoothDevice bluetoothDevice=iterator.next();
            list.add(bluetoothDevice);
        }
        return list;
    }
    public void initData(BluetoothAdapter bluetoothAdapter){
        if(bluetoothAdapter==null||!bluetoothAdapter.isEnabled()){
            return ;
        }
        Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
        if(devices.size()>0){
            Iterator<BluetoothDevice> iterator=devices.iterator();
            while(iterator.hasNext()){
                BluetoothDevice bluetoothDevice=iterator.next();
                bluetoothDeviceList.add(bluetoothDevice);
            }
            adapter.notifyDataSetChanged();
        }
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    public void show(View viewToShow){
        if(viewToShow.getId()==progressingBg.getId()){
            errormsgBg.setVisibility(View.GONE);
            progressingBg.setVisibility(View.VISIBLE);
        }else if(viewToShow.getId()==errormsgBg.getId()){
            progressingBg.setVisibility(View.GONE);
            errormsgBg.setVisibility(View.VISIBLE);
        }else if(viewToShow.getId()==listviewBg.getId()){
            errormsgBg.setVisibility(View.GONE);
            progressingBg.setVisibility(View.GONE);
        }
    }
    public void show(View viewToShow,String errorTitle,String errorDesc){
        if(viewToShow.getId()==progressingBg.getId()){
            errormsgBg.setVisibility(View.GONE);
            progressingBg.setVisibility(View.VISIBLE);
        }else if(viewToShow.getId()==errormsgBg.getId()){
            progressingBg.setVisibility(View.GONE);
            errormsgBg.setVisibility(View.VISIBLE);
        }

        if(viewToShow.getId()==errormsgBg.getId()){
            title.setText(errorTitle);
            desc.setText(errorDesc);
        }
        if(viewToShow.getId()==progressingBg.getId()){
            progressTitle.setText(errorTitle);
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

}
